import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ServletSession extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        Cookie[] cookies=request.getCookies();
//        for (int i = 0; i < cookies.length; i++) {
//            if ("JSESSIONID".equals(cookies[i].getName())){
//                response.getWriter().write(cookies[i].getValue());
//            }
//        }
        HttpSession session=request.getSession();
        String cart=(String)session.getAttribute("cart");
        if(cart==null){
            //首次访问这个服务器，还没有在服务器端创建seesion
            session.setAttribute("cart","这是服务器保存的内容："+session.getId());
        }
        System.out.println(session.getId());
        Cookie cookie=new Cookie("JSESSIONID",session.getId());
        cookie.setMaxAge(60);
        response.addCookie(cookie);
        response.getWriter().write("服务器端吧保存的内容："+session.getId());
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
